package com.nbicc.ad.controller;

import com.nbicc.ad.po.AdInstance;
import com.nbicc.ad.service.IAdInstanceService;
import com.nbicc.ad.vo.AdInstanceQueryCondition;
import com.nbicc.common.RequestPageObj;
import com.nbicc.common.ResponseObj;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Api(value = "广告实例" , description = "单条广告的操作")
@RestController
@RequestMapping(value = "/ad/instance")
public class AdInstanceController {

    @Resource(name = "AdInstanceService")
    private IAdInstanceService adInstanceService;

    @ApiOperation(value = "按id查询单条广告")
    @GetMapping("/id/{id}")
    @ResponseBody
    public ResponseObj selectById(@PathVariable int id) throws Exception {
        AdInstance adInstance = adInstanceService.selectById(id);
        return ResponseObj.success().setResult(adInstance);
    }

    @ApiOperation(value = "添加单条广告")
    @PostMapping
    @ResponseBody
    public ResponseObj insert(@RequestBody AdInstance adInstance) throws  Exception{
        adInstanceService.insert(adInstance);
        return ResponseObj.success().setResult(adInstance);
    }

    @ApiOperation(value = "修改单条广告")
    @PutMapping
    @ResponseBody
    public ResponseObj update(@RequestBody Map<String,Object> adInstance) throws Exception{
        adInstanceService.updateById(adInstance);
        return ResponseObj.success();
    }

    @ApiOperation(value = "删除单条广告", notes = "只需包含广告id")
    @DeleteMapping("/id/{id}")
    @ResponseBody
    public ResponseObj delete(@PathVariable int id){
        adInstanceService.deleteById(id);
        return ResponseObj.success();
    }

    //↑crud========================================================================

    @ApiOperation(value = "按条件(分页等) 广告实例", notes = "分页和条件查询")
    @PostMapping("/find")
    @ResponseBody
    public ResponseObj find(@RequestBody RequestPageObj<Map<String,Object>> requestPageObj){
        ResponseObj responseObj = adInstanceService.find(requestPageObj);
        return responseObj;
    }

    @ApiOperation(value = "管理端按需求展示所有")
    @PostMapping("/list")
    public List<AdInstance> selectListByCatInCurrent(@RequestBody AdInstanceQueryCondition adInstanceQueryCondition){
        List<AdInstance> adInstanceList = adInstanceService.selectListByAdInstanceQueryCondition(adInstanceQueryCondition);
        return adInstanceList;
    }

}
